#!/usr/bin/python
'''This script is a translator from Seb way to name sprites to mine'''
import re
import sys
import os
import shutil
arg = sys.argv
print arg
def replace(dirname, destname):
	for fname in os.listdir("./"+dirname):
		m = re.match("(\w*)(\d\d\d\.png)", fname)
		if(m != None):
			shutil.move("./"+dirname+"/"+fname, "./"+dirname+"/"+destname+m.group(2))
def seb(dirname):
	table = {'B': '4', 'H': '3', 'G':'2', 'D':'1'}
	for fname in os.listdir("./"+dirname):
		m = re.match("([A-Za-z]+)([A-Za-z])(\d)(\.png)", fname)
		if(m != None):
			name = m.group(1).lower()
			direction = table[m.group(2)]
			number = int(m.group(3))
			rest = m.group(4)
			if((number == 1) or (number == 5)):
				shutil.copy("./"+dirname+"/"+fname, dirname+"/"+name+str(number%3)+direction+'3'+rest)
				shutil.move("./"+dirname+"/"+fname, dirname+"/"+name+str(number%3)+direction+'1'+rest)
			elif((number%4 == 2) or (number % 4 == 3)):
				state = ((number %4)!=number)+1
				n = (number == 3)+2
				shutil.move("./"+dirname+"/"+fname, dirname+"/"+name+str(state)+direction+str((number == 3)*2+2)+rest)
			elif(number == 4):
				state = '3'
				shutil.move("./"+dirname+"/"+fname, "./"+dirname+"/"+name+state+direction+'1'+rest)
		else:
			print "m is None"
def addnumber(name, number):
	m = re.match("([\w\/])[\/](\w+)", name)
	dirname = m.group(1)
	cname = m.group(2)
	for fname in os.listdir("./"+dirname):
		m = re.match("(\w+)(\d\d\d\.png)", fname)
		if(m != None):
			if(cname == m.group(1)):
				shutil.move("./"+dirname+"/"+fname, "./"+dirname+"/"+cname+str(number)+m.group(2))
			
if ((arg[1] == "replace") and (len(arg) == 4)):
	try:
		replace(arg[2], arg[3])
	except OSError:
		print "Problem with directory"
elif ((arg[1] == "seb") and (len(arg) == 3 )):
	seb(arg[2])
elif ((arg[1] =="addnumber") and (len(arg)==4)):
	addnumber(arg[2], arg[3])
else:
	print "Error"
